/****************************************
    MainWindows (longdh ui)
****************************************/

import QtQuick 2.1
import QtQuick.Layouts 1.0
import QtQuick.Controls 1.0
import QtQuick.Controls.Styles 1.0
import org.qtproject.znews_qt 1.0

import "common"
import "page"
import "articlecontent"


ApplicationWindow {
    id: root
    height: 640
    width: 480
    title: "News Reader"

//=====================================================================================
    property string statusBarMessage

    property string linkContentImage;

    property Component defaultPage: WaitPage {

    }

    property Component contentImage: ContentImage {
        sourceImage: linkContentImage

        onPreviousPage: if (!m_isLocked) {
                            m_isLocked = true;
                            pageView.pop();
                        }

    }


    property alias m_contentArticle: id_ContentArticle
    ContentArticle {
        id: id_ContentArticle
        onPreviousPage: if (!m_isLocked) {
                            m_isLocked = true;
                            pageView.pop();
                        }

        onPushImage: {
            console.info(" push Image............ ");
            linkContentImage = linkImage;

            pageView.push(contentImage);

        }

    }

    property Component listPage: MainPage {
        onNextPage: if (!m_isLocked) {
                        console.info(" show ContentArticle ");
                        m_isLocked = true;
                        pageView.push(m_contentArticle);
                    }



    }
 //=====================================================================================
    // cai nay lam j phai ?
    function openingUpdateSuccess() {
        pageView.pop();
        pageView.push(listPage);
    }
//=====================================================================================

    StackView {
        id: pageView
        anchors.fill: parent
        focus: true

        initialItem: defaultPage

        delegate: StackViewDelegate {
            pushTransition: StackViewTransition {
                function transitionFinished(properties)
                {
                    properties.exitItem.opacity = 1
                }
                PropertyAnimation {
                    target: enterItem
                    property: "x"
                    from: target.width
                    to: 0
                    duration: 500
                    easing.type: Easing.OutSine
                }
                PropertyAnimation {
                    target: exitItem
                    property: "x"
                    from: 0
                    to: -target.width
                    duration: 500
                    easing.type: Easing.OutSine
                }
            }
            popTransition: StackViewTransition {
                function transitionFinished(properties)
                {
                    properties.exitItem.opacity = 1
                }
                PropertyAnimation {
                    target: enterItem
                    property: "x"
                    from: -target.width
                    to: 0
                    duration: 500
                    easing.type: Easing.OutSine
                }
                PropertyAnimation {
                    target: exitItem
                    property: "x"
                    from: 0
                    to: target.width
                    duration: 500
                    easing.type: Easing.OutSine

                }
            }
            property Component replaceTransition: pushTransition
        }
    }

    statusBar: StatusBar {
        width: parent.width
        opacity: label.text !== "" ? 1 : 0
        height: label.text !== "" ? 65 * ApplicationInfo.ratio : 0

        Behavior on height { NumberAnimation {easing.type: Easing.OutSine}}
        Behavior on opacity { NumberAnimation {}}

        style: StatusBarStyle {
            padding { left: 0; right: 0 ; top: 0 ; bottom: 0}
            property Component background: Rectangle {
                implicitHeight: 65 * ApplicationInfo.ratio
                implicitWidth: root.width
                color: ApplicationInfo.colors.smokeGray
                Rectangle {
                    width: parent.width
                    height: 1
                    color: Qt.darker(parent.color, 1.5)
                }
                Rectangle {
                    y: 1
                    width: parent.width
                    height: 1
                    color: "white"
                }
            }
        }
        TouchLabel {
            id: label
            y: 32 * ApplicationInfo.ratio - height/2
            width: parent.width // The text will only wrap if an explicit width has been set
            text: statusBarMessage
            textFormat: Text.RichText
            onLinkActivated: Qt.openUrlExternally(link)
            wrapMode: Text.Wrap
            pixelSize: 18
            letterSpacing: -0.15
            color: ApplicationInfo.colors.mediumGray
            verticalAlignment: Text.AlignVCenter
            horizontalAlignment: Text.AlignHCenter
        }
    }
//=====================================================================================
    Component.onCompleted:  {
        ApplicationInfo.initData();
        ApplicationInfo.openingUpdateSuccess.connect(openingUpdateSuccess)
    }

}
